package com.qut.graduate.busrtquery.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.adapter.TransferPathPlanAdapter;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;
import com.qut.graduate.busrtquery.ui.MainActivity;
import com.qut.graduate.busrtquery.utils.common.LogUtils;

import java.util.ArrayList;

/**
 * @Author WLL
 * @Time: 2019/3/23 16:39
 */
public class TransferFragment extends BaseFragment {
    private static final String TAG = "TransferFragment";
    RoutePlanSearch mSearch = null; // 搜索模块，可去掉地图模块独立使用
    private LinearLayout mTopLayout;
    private ImageButton mChangeAddressBtn;
    private TextView mStartAddress;
    private TextView mEndAddress;
    private String mStartName = "";
    private String mEndName = "";
    private LatLng startLatLng;
    private LatLng endLatLng;
    private RecyclerView mHistoryRecyclerView;
    private RecyclerView mRouteRecyclerView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_transfer_query;
    }

    protected void init() {
        initView();
    }

    // 设置换乘界面顶部的起点、终点信息及其经纬度
    public void setStartEndAddress(String startPlaceName, String endPlaceName, LatLng mCurrentLatLng, LatLng mAimLatLng) {
        // 从导航界面跳转到换乘界面，起点设置为“我的位置”，从selectActivity则默认原来的地点名称
        if (startPlaceName != null)
            mStartName = startPlaceName;
        mStartAddress.setText(mStartName);
        if (endPlaceName != null)
            mEndName = endPlaceName;
        mEndAddress.setText(mEndName);
        if (mCurrentLatLng != null)
            startLatLng = mCurrentLatLng;
        if (mAimLatLng != null)
            endLatLng = mAimLatLng;
        LogUtils.d("----从导航界面跳转到换乘界面----startLatLng: " + startLatLng + "----endLatLng: " + endLatLng, getClass());
    }

    @Override
    public void onResume() {
        super.onResume();
        mStartAddress.setText(mStartName);
        mEndAddress.setText(mEndName);
    }

    public void setBusRoutePlan(final ArrayList<TransitRouteLine> transitRouteLines) {
        mHistoryRecyclerView.setVisibility(View.GONE);
        mRouteRecyclerView.setVisibility(View.VISIBLE);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRouteRecyclerView.setLayoutManager(manager);
        if (transitRouteLines != null)
            LogUtils.d(String.valueOf(transitRouteLines.size()), getClass());
        TransferPathPlanAdapter transferPathPlanAdapter = new TransferPathPlanAdapter(getActivity(), transitRouteLines, BRTQConstant.TRANSIT_ROUTE);
        mRouteRecyclerView.setAdapter(transferPathPlanAdapter);
        transferPathPlanAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                PlanNode stNode = PlanNode.withLocation(startLatLng);
                PlanNode enNode = PlanNode.withLocation(endLatLng);
                LogUtils.d("----换乘界面----stNode: " + stNode.getLocation() + "----enNode: " + enNode.getLocation(), getClass());
                ((MainActivity) getActivity()).gotoBusRouteDetail(transitRouteLines, position, stNode, enNode);
            }
        });
        transferPathPlanAdapter.notifyDataSetChanged();
    }

    private void initView() {
        mTopLayout = mRootView.findViewById(R.id.transfer_top_layout);
        mChangeAddressBtn = mRootView.findViewById(R.id.transfer_change_address_btn);
        mStartAddress = mRootView.findViewById(R.id.transfer_start_address);
        mEndAddress = mRootView.findViewById(R.id.transfer_end_address);
        mHistoryRecyclerView = mRootView.findViewById(R.id.transfer_history_recyclerView);
        mRouteRecyclerView = mRootView.findViewById(R.id.transfer_route_recyclerView);

        mStartAddress.setOnClickListener(this);
        mEndAddress.setOnClickListener(this);
        mChangeAddressBtn.setOnClickListener(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.transfer_start_address:
                ((MainActivity) getActivity()).gotoSelectAddress(BRTQConstant.SELECT_PLACE_TYPE_START, mStartName, mEndName, startLatLng, endLatLng);
                break;
            case R.id.transfer_end_address:
                ((MainActivity) getActivity()).gotoSelectAddress(BRTQConstant.SELECT_PLACE_TYPE_END, mStartName, mEndName, startLatLng, endLatLng);
                break;
            case R.id.transfer_change_address_btn:
                changeRouteDirection();
                break;
        }
    }

    /**
     * 切换起点、终点，重新规划路径
     */
    public void changeRouteDirection() {
        String tempName = mStartName;
        mStartName = mEndName;
        mEndName = tempName;
        LatLng tempLatLng = startLatLng;
        startLatLng = endLatLng;
        endLatLng = tempLatLng;
        mStartAddress.setText(mStartName);
        mEndAddress.setText(mEndName);
        if (startLatLng != null && endLatLng != null)
            ((MainActivity) getActivity()).searchTransitRoute(startLatLng, endLatLng);
    }

}